package com.webserver5.controller;

import com.webserver5.annotations.RequestMapping;
import com.webserver5.entity.User;
import com.webserver5.http.HttpServletRequest;
import com.webserver5.http.HttpServletResponse;
import com.webserver5.util.DBUtil;

import java.io.*;
import java.sql.*;

public class UserController {
    private static File userDir;
    static {
        userDir = new File("/users");
        if(userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理注册...");
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        String nickname = request.getParameters("nickname");
        String ageStr = request.getParameters("age");
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty() ||
                nickname == null || nickname.isEmpty() ||
                ageStr == null || ageStr.isEmpty() || ageStr.matches("[0-9]+")) {
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        System.out.println(username + " " + password + " " + nickname + " " + ageStr);
        int age = Integer.parseInt(ageStr);
        try (
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT 1 FROM userinfo WHERE username = ''"+username+"''";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                System.out.println("该用户已注册");
                response.sendRedirect("/have_user.html");
                return;
            }
            sql = "INSERT INTO userinfo (username,password,nickname,age) VALUES ('"+username+"','"+password+"'','"+nickname+"',"+age+")";
            PreparedStatement ps = connection.prepareStatement(sql);
            int num = ps.executeUpdate();
            if (num>0){
                response.sendRedirect("/reg_success.html");
                System.out.println("恭喜你，注册成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void login(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        if (username==null || username.isEmpty() ||
        password == null || password.isEmpty()
        ){
            System.out.println("登录信息输入格式有误，请重新输入");
            response.sendRedirect("login_info_error.html");
            return;
        }
        try (
                Connection connection = DBUtil.getConnection();
                ){
            String sql = "SELECT username,password FROM useronfo WHERE username=?'"+username+"',password=?'"+password+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                System.out.println("登陆成功");
                response.sendRedirect("login_success.html");
            }else {
                System.out.println("登陆失败");
                response.sendRedirect("login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
